﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Profile.cs" company="DissingNelleman">
//   
// All rights reserved. </copyright>
// <summary>
//   The profile.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ServiceLibrary.Services.Dto
{
    using System;
    using System.Runtime.Serialization;

    /// <summary>
    /// The profile.
    /// </summary>
    [DataContract]
    public class Profile
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the cvr.
        /// </summary>
        [DataMember]
        public string CVR { get; set; }

        /// <summary>
        /// Gets or sets the contact type.
        /// </summary>
        [DataMember]
        public Guid? ContactType { get; set; }

        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        [DataMember]
        public Guid ID { get; set; }

        /// <summary>
        /// Gets or sets the location.
        /// </summary>
        [DataMember]
        public Guid Location { get; set; }

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        [DataMember]
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the relation.
        /// </summary>
        [DataMember]
        public Guid Relation { get; set; }

        /// <summary>
        /// Gets or sets the sub type.
        /// </summary>
        [DataMember]
        public Guid SubType { get; set; }

        /// <summary>
        /// Gets or sets if it's within 150km.
        /// </summary>
        [DataMember]
        public bool? Within150Km { get; set; }

        /// <summary>
        /// Gets or sets if it's national.
        /// </summary>
        [DataMember]
        public bool? IsRemoteArea { get; set; }

        /// <summary>
        /// Gets or sets if it's an entrepreneur.
        /// </summary>
        [DataMember]
        public bool? IsEntrepreneur { get; set; }

        /// <summary>
        /// Gets or sets the sub type.
        /// </summary>
        [DataMember]
        public Guid Region { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The convert.
        /// </summary>
        /// <param name="profile">
        /// The profile.
        /// </param>
        /// <returns>
        /// The <see cref="Profile"/>.
        /// </returns>
        public static Profile Convert(Models.Domain.Profile profile)
        {
            if (profile != null)
            {
                var dto = new Profile
                              {
                                  ID = profile.ID,
                                  Location = profile.Location != null ? profile.Location.ID : Guid.Empty,
                                  Region = profile.Region != null ? profile.Region.ID : Guid.Empty,
                                  Relation = profile.Relation.ID,
                                  SubType = profile.SubType.ID,
                                  CVR = profile.CVR,
                                  IsEntrepreneur = profile.IsEntrepreneur,
                                  IsRemoteArea = profile.IsRemoteArea,
                                  Within150Km = profile.Within150km,
                                  Name = profile.Name
                              };

                if (profile.ContactType != null)
                {
                    dto.ContactType = profile.ContactType.ID;
                }

                return dto;
            }

            return null;
        }

        #endregion
    }
}

namespace ServiceLibrary.Services.Queries
{
    using System;
    using System.Runtime.Serialization;

    using NHibernate;

    using ServiceLibrary.Actioners.Profiles;
    using ServiceLibrary.Services.Results;

    /// <summary>
    /// The profiles query.
    /// </summary>
    [DataContract]
    public class ProfilesQuery : IQuery
    {
        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <returns>
        /// The <see cref="IResult"/>.
        /// </returns>
        public override IResult Execute(ISession hsession)
        {
            var actioner = new ProfileActioner(hsession, null);
            return actioner.ExecuteSearchQuery(this);
        }

        #endregion
    }

    /// <summary>
    /// The profiles query.
    /// </summary>
    [DataContract]
    public class ProfileIsReferencedQuery : IQuery
    {
        #region Public Methods and Operators

        [DataMember]
        public Guid ID { get; set; }

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <returns>
        /// The <see cref="IResult"/>.
        /// </returns>
        public override IResult Execute(ISession hsession)
        {
            var actioner = new ProfileActioner(hsession, null);
            var res = new ProfileIsReferencedResult();
            var isprofileReferenced = actioner.IsProfileReferenced(this.ID);
            res.IsReferenced = isprofileReferenced.Item1;
            res.List = isprofileReferenced.Item2;

            return res;
        }

        #endregion
    }
}

namespace ServiceLibrary.Services.Results
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.Serialization;

    using ServiceLibrary.Enums;
    using ServiceLibrary.Services.Dto;

    /// <summary>
    /// The profiles result.
    /// </summary>
    [DataContract]
    public class ProfilesResult : IResult
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ProfilesResult"/> class.
        /// </summary>
        public ProfilesResult()
        {
            this.List = new List<Profile>();
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets the list.
        /// </summary>
        [DataMember]
        public List<Profile> List { get; set; }

        /// <summary>
        /// Gets or sets the total row count.
        /// </summary>
        [DataMember]
        public int TotalRowCount { get; set; }

        #endregion
    }

    /// <summary>
    /// The profiles result.
    /// </summary>
    [DataContract]
    public class ProfileIsReferencedResult : IResult
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ProfileIsReferencedResult"/> class. 
        /// </summary>
        public ProfileIsReferencedResult()
        {
            this.List = new List<Tuple<Guid, LogEntryType>>();
        }

        #endregion

        /// <summary>
        /// Gets or sets the list.
        /// </summary>
        [DataMember]
        public List<Tuple<Guid, LogEntryType>> List { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether it is referenced or not.
        /// </summary>
        [DataMember]
        public bool IsReferenced { get; set; }
    }
}

namespace ServiceLibrary.Services.Commands
{
    using System;
    using System.Runtime.Serialization;

    using NHibernate;

    using ServiceLibrary.Actioners.Profiles;
    using ServiceLibrary.Services.Dto;

    using User = ServiceLibrary.Models.Domain.User;

    /// <summary>
    /// The create profile command.
    /// </summary>
    [DataContract]
    public class CreateProfileCommand : ICommand
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the profile.
        /// </summary>
        [DataMember]
        public Profile Profile { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public override void Execute(ISession hsession, User activeUser)
        {
            var actioner = new ProfileActioner(hsession, activeUser);
            actioner.CreateProfile(this);
        }

        #endregion
    }

    /// <summary>
    /// The update profile command.
    /// </summary>
    [DataContract]
    public class UpdateProfileCommand : ICommand
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the updated profile.
        /// </summary>
        [DataMember]
        public Profile UpdatedProfile { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public override void Execute(ISession hsession, User activeUser)
        {
            var actioner = new ProfileActioner(hsession, activeUser);
            actioner.UpdateProfile(this);
        }

        #endregion
    }

    /// <summary>
    /// The delete profile command.
    /// </summary>
    [DataContract]
    public class DeleteProfileCommand : ICommand
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the id of the profile to delete.
        /// </summary>
        [DataMember]
        public Guid? ID { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public override void Execute(ISession hsession, User activeUser)
        {
            var actioner = new ProfileActioner(hsession, activeUser);
            actioner.DeleteProfile(this);
        }

        #endregion
    }
}